package name.manana.jarina;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

/**
 * The Win class represents top-level window of the GUI interface for the
 * Jarina.
 * 
 * @author Michal Bouska
 * 
 */
public class Win extends JFrame {
	
	private static final Logger log = Logger.getLogger(Win.class);

	private static final long serialVersionUID = 3348409100498682338L;

	private static final String DIR_DESC = "directory";

	private String TITLE = "Jarina";

	private JButton runButton = new JButton("RUN!");

	private JButton exitButton = new JButton("EXIT");

	private JButton mvnRepoButton = new JButton("choose maven repository");

	private JButton projectButton = new JButton("choose project dir");

	private JTextField mvnRepo = new JTextField(35);

	private JTextField projectDir = new JTextField(35);

	/**
	 * Construct the main window.
	 */
	Win() {
		init();
	}

	/**
	 * Initialize components.
	 */
	public void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(TITLE);

		// JMenuBar
		JMenuBar menu = new JMenuBar();
		JMenu file = new JMenu("File");
		JMenuItem exitWin = new JMenuItem("Exit");
		file.add(exitWin);
		menu.add(file);
		setJMenuBar(menu);

		// create main panel
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());

		// labels buttons in the north panel
		JLabel mvnRepoLabel = new JLabel("Maven repository:");
		JLabel projectLabel = new JLabel("Project folder:");
		final Dimension labelDimension = new Dimension(115, 20);
		mvnRepoLabel.setPreferredSize(labelDimension);
		projectLabel.setPreferredSize(labelDimension);
		final Dimension buttonDimension = new Dimension(200, 25);
		mvnRepoButton.setPreferredSize(buttonDimension);
		projectButton.setPreferredSize(buttonDimension);

		JPanel northPanel = new JPanel();
		northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.Y_AXIS));

		JPanel row1Panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		row1Panel.add(mvnRepoLabel);
		row1Panel.add(mvnRepo);
		row1Panel.add(mvnRepoButton);

		JPanel row2Panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		row2Panel.add(projectLabel);
		row2Panel.add(projectDir);
		row2Panel.add(projectButton);

		northPanel.add(row1Panel);
		northPanel.add(row2Panel);

		// south panel
		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		southPanel.add(runButton);
		southPanel.add(exitButton);

		mainPanel.add(northPanel, BorderLayout.NORTH);
		mainPanel.add(southPanel, BorderLayout.SOUTH);
		setContentPane(mainPanel);
		setSize(750, 250);

		// add listeners
		exitWin.addActionListener(new ExitListener());
		exitButton.addActionListener(new ExitListener());
		projectButton.addActionListener(new OpenFileAction());
		mvnRepoButton.addActionListener(new OpenFileAction());
		runButton.addActionListener(new RunAction());
	}

	/**
	 * Class that process run action.
	 */

	private class RunAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String mvnRepository = mvnRepo.getText();
			String projectDirectory = projectDir.getText();
			MainApp.main(new String[] { projectDirectory, mvnRepository });
		}
	}

	/**
	 * Class that process exit action. Dispose the window.
	 */
	private class ExitListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			dispose();
		}
	}

	/**
	 * Class that process open file chooser dialog.
	 */
	private class OpenFileAction implements ActionListener {

		final JFileChooser fileChooser = new JFileChooser();

		public void actionPerformed(ActionEvent e) {
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fileChooser.setCurrentDirectory(new File("C:/"));
			fileChooser.setFileFilter(new FileFilter() {
				@Override
				public boolean accept(File dir) {
					if (dir.isDirectory()) {
						return true;
					}
					return false;
				}

				@Override
				public String getDescription() {
					return DIR_DESC;
				}

			});
			if (fileChooser.showOpenDialog(Win.this) == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				String absolutePath = file.getAbsolutePath();
				if (e.getSource() == mvnRepoButton) {
					mvnRepo.setText(absolutePath);
				} else if (e.getSource() == projectButton) {
					projectDir.setText(absolutePath);
				}
			}
		}
	}

}
